/*
 * @(#)StandardStorageLocationResolver.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.dms.core;

import java.sql.Connection;
import java.util.List;

import com.dcivision.dms.bean.DmsLocMaster;
import com.dcivision.dms.bean.DmsRoot;
import com.dcivision.dms.dao.DmsLocMasterDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.bean.BeansSorter;

/**
  StandardStorageLocationResolver.java

  This class is providing a stardard location resolve method for DMS stroage location.

/**
 * <p>Class Name:       StandardStorageLocationResolver.java    </p>
 * <p>Description:      This class is providing a stardard location resolve method for DMS stroage location.</p>
 * @author              Zoe Shum
 * @company             DCIVision Limited
 * @creation date       05/08/2003
 * @version             $Revision: 1.10 $
*/

public class StandardStorageLocationResolver implements DmsStorageLocationResolver{

  public static final String REVISION = "$Revision: 1.10 $";


  public StandardStorageLocationResolver() {
  }

  /**
   * Gets dmsLocMaster ID for create the storage location of personal folder
   * <P>
   * Override the parent's function. Purpose in create the default storage location for users
   * </P>
   *
   * @param userRecordID       ther user record ID
   * @param sessionContainer   the sessionContainer object
   * @param conn               The valid db connection
   * @throws ApplicationException
         */
  public Integer resolvePersonalStorageLoc(Integer userRecordID, SessionContainer sessionContainer, Connection conn) throws ApplicationException {
    Integer dmsStorageLoc = new Integer (0);
    DmsLocMasterDAObject dmsLocMasterDAO = new DmsLocMasterDAObject(sessionContainer, conn);
    List dmsLocList = dmsLocMasterDAO.getFullList();
    
    // Hardcode to return "1" as Default for Personal Storage.
    dmsStorageLoc = new Integer(1);
/*    
    if (dmsLocList.size()==1) {
      DmsLocMaster dmsLocMaster = (DmsLocMaster) dmsLocList.get(0);
      dmsStorageLoc = dmsLocMaster.getID();
    } else{
      //Special Location Resolve Method HERE
    }
*/    
    dmsLocMasterDAO=null;
    return dmsStorageLoc;
  }

  /**
   * Gets dmsLocMaster ID for create the storage location of the publlic root
   * <P>
   * Override the parent's function. Purpose in create the default storage location for Public Folder
   * </P>
   * @param userRecord
   * @param form
   * @param request
   * @param response
   * @throws ApplicationException
   */
  public Integer resolvePublicStorageLoc(DmsRoot dmsRoot, SessionContainer sessionContainer, Connection conn) throws ApplicationException {
    Integer dmsStorageLoc = new Integer(0);
    DmsLocMasterDAObject dmsLocMasterDAO = new DmsLocMasterDAObject(sessionContainer, conn);
    List dmsLocList = dmsLocMasterDAO.getFullList();
    if (dmsLocList.size()==1) {
      DmsLocMaster dmsLocMaster = (DmsLocMaster) dmsLocList.get(0);
      dmsStorageLoc = dmsLocMaster.getID();
    } else{
      //Special Location Resolve Method HERE
      
      /** for handling >2 loc master record and get the most updated one */
      if(dmsLocList.size()!=0){
        dmsLocList=BeansSorter.sortDescending(dmsLocList,"ID");
        DmsLocMaster dmsLocMaster = (DmsLocMaster) dmsLocList.get(0);
        dmsStorageLoc = dmsLocMaster.getID();
      }
    }
    dmsLocMasterDAO=null;
    return dmsStorageLoc;
  }
}
